package com.yitiao.multithread.case02;

/**
 * <p>interrupted 方法在进行中断的时候会抛出一个 java.lang.InterruptedException 的异常，
 * 但是被打断的线程在抛出异常之后依旧会正常执行任务</p>
 * <p>采用 interrupted 函数并不能真正地将线程中断，只能告知线程，目前需要进入中断状态，
 * 然后修改线程的状态为停止状态，但是接下来的处理流程得由线程自己去决定。</p>
 */
public class InterruptThread {
    public static void main(String[] args) throws InterruptedException {
        Thread testThread = new Thread(new ThreadType.TestThread());
        testThread.start();
        Thread testThreadWithSync = new Thread(new ThreadType.TestThreadWithSync());
        testThreadWithSync.start();
        Thread testThreadWithLock = new Thread(new ThreadType.TestThreadWithLock());
        testThreadWithLock.start();
        Thread forEverThread = new Thread(new ThreadType.ForEverThread());
        forEverThread.start();

        Thread.sleep(1000);

        // 如果线程是正常状态，在调用interrupt会继续正常执行
        forEverThread.interrupt();

        // 如果当前线程是阻塞（sleep,join,wait,condition.await）状态，在调用interrupt会抛出异常
        // java.lang.InterruptedException: sleep interrupted
        testThread.interrupt();
        testThreadWithSync.interrupt();
        testThreadWithLock.interrupt();
        Thread.sleep(1000);
    }
}
